$(function() {
    var m={
        location:null,
        maintain:$(".maintain"),
        table:$(".maintain table"),
        typeForm:$("#typeForm"),
        subtypeForm:$("#subtypeForm"),
        strengthForm:$("#strengthForm"),
        processForm:$("#processForm"),
        processInfoForm:$("#processInfoForm"),
        currentPage:$(".currentPage"),
        totalPage:$(".totalPage"),
        xSearch:$(".x-search"),
        pageWidget:$(".page-widget"),
        disasterItem:$(".disasterItem"),
        forecast:$(".forecast"),
        contentRight:$(".content-right"),
    }

    var type={
        tableHtml:function(data){
            var html='<thead><tr><th class="displayNone"></th><th>序号</th><th>灾害名称</th><th>特征描述</th><th>致灾原因</th></tr></thead><tbody>';
            for(var i=0;i<data.content.length;i++){
                var obj=data.content[i];
                html+=`
					<tr><td class="displayNone">${obj.id}</td><td>${i+1}</td><td>${obj.name}</td><td>${obj.signalment}</td><td>${obj.disasterReason}</td></tr>`;
            }
            html+="</tbody>";
            m.table.html(html);
        },
        ajaxType:function(pageNum){
            var that=this;
            $.ajax({
                type:"get",
                url:"/type/getTypes",
                data:{pageNum:pageNum},
                success:function(data){
                    that.tableHtml(data);
                    m.totalPage.html(data.totalPage);
                }
            });
        },
        displayType:function(pageNum){
            m.maintain.show();
            m.maintain.children().hide();
            m.table.show();
            m.xSearch.hide();
            m.forecast.hide();
            m.pageWidget.show();
            m.table.addClass("disasterType");
            m.table.removeClass("disasterInfo");
            m.table.removeClass("disasterProcess");
            m.disasterItem.show();
            m.disasterItem.html("灾害类型查看");
            m.currentPage.html(1);
            //加载灾害类型---首次加载，默认显示
            this.ajaxType(pageNum);
        },
        add:function(){
            $(".content-left ul li").eq(3).addClass("selected").siblings().removeClass("selected");
            m.maintain.show();
            m.maintain.children().hide();
            m.xSearch.hide();
            m.pageWidget.hide();
            m.forecast.hide();
            m.typeForm.show();
            m.disasterItem.show();
            m.disasterItem.html("新建灾害类型");
        }
    }
    var process={
        tableHtml:function(data){
            var html='<thead><tr><th class="displayNone"></th><th>序号</th><th>乡镇名称</th><th>开始时间</th><th>结束时间</th></tr></thead><tbody>';
            for(var i=0;i<data.content.length;i++){
                var obj=data.content[i];
                html+=`
					<tr><td class="displayNone">${obj.id}</td><td>${i+1}</td><td>${obj.xsAreaName}</td><td>${obj.startTime}</td><td>${obj.endTime}</td></tr>`;
            }
            html+="</Tbody>";
            m.table.html(html);
        },
        ajaxProcess:function(pageNum){
            var that=this;
            $.ajax({
                type:"get",
                url:"/process/getProcesses",
                data:{pageNum:pageNum},
                success:function(data){
                    that.tableHtml(data);
                    m.totalPage.html(data.totalPage);
                }
            });
        },
        display:function(pageNum){
            $(".content-left ul li").eq(1).addClass("selected").siblings().removeClass("selected");
            m.maintain.show();
            m.maintain.children().hide();
            m.table.show().html("");
            m.xSearch.show();
            m.pageWidget.show();
            m.forecast.hide();
            m.table.removeClass("disasterType");
            m.table.removeClass("disasterInfo");
            m.table.addClass("disasterProcess");
            m.disasterItem.show();
            m.disasterItem.html("灾害过程查看");
            m.currentPage.html(1);
            //加载灾害过程
            this.ajaxProcess(pageNum);

        },
        add:function(){
            $(".content-left ul li").eq(4).addClass("selected").siblings().removeClass("selected");
            m.maintain.show();
            m.maintain.children().hide();
            m.xSearch.hide();
            m.pageWidget.hide();
            m.forecast.hide();
            m.processForm.show();
            m.disasterItem.show();
            m.disasterItem.html("新建灾情过程");
        }
    }
    var info={

        tableHtml:function(data){
            var html='<thead><tr><th class="displayNone"></th><th>序号</th><th>乡镇名称</th><th>开始时间</th><th>结束时间</th></tr></thead><tbody>';
            for(var i=0;i<data.content.length;i++){
                var obj=data.content[i];
                html+=`
					<tr><td class="displayNone">${obj.id}</td><td>${i+1}</td><td>${obj.xsAreaName}</td><td>${obj.startTime}</td><td>${obj.endTime}</td></tr>`;
            }
            html+="</Tbody>";
            m.table.html(html);
        },
        ajaxInfo:function(pageNum){
            var that=this;
            $.ajax({
                type:"get",
                url:"/process/getInfos",
                data:{pageNum:pageNum},
                success:function(data){
                    that.tableHtml(data);
                    m.totalPage.html(data.totalPage);
                }
            });
        },
        display:function(pageNum){
            $(".content-left ul li").eq(2).addClass("selected").siblings().removeClass("selected");
            m.maintain.show();
            m.maintain.children().hide();
            m.table.show().html("");
            m.xSearch.show();
            m.pageWidget.show();
            m.forecast.hide();
            m.table.removeClass("disasterType");
            m.table.addClass("disasterInfo");
            m.table.removeClass("disasterProcess");
            m.disasterItem.show();
            m.disasterItem.html("灾害信息查看");
            m.currentPage.html(1);
            //加载灾害信息
            this.ajaxInfo(pageNum);

        }
    }
    var forecast={
        update(id,threshold){
            $.ajax({
                type:"get",
                data:{id:id,threshold:threshold},
                url:"/forecast/update",
                success:function(data){
                    console.log("成功");
                }
            })
        }
    }



    //加载灾害类型---首次加载，默认显示
    type.ajaxType(1);




    m.location=c.locationHref();
    if(m.location[1]=="0"){
        type.displayType(m.location[3]);
        m.currentPage.html(m.location[3]);
    }
    if(m.location[1]=="1"){
        process.display(m.location[3]);
        m.currentPage.html(m.location[3]);
    }
    if(m.location[1]=="2"){
        info.display(m.location[3]);
        m.currentPage.html(m.location[3]);
    }
    if(m.location[1]=="3"){
        type.add();
    }
    if(m.location[1]=="4"){
        process.add();
    }
    if(m.location[1]=="5"){
        setForecast();
    }






    //插件--序列化--提交表单
    $.fn.serializeObject = function(){
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name]) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };
    function submitForm(){
        var arg=arguments;
        var data = arg[0].serializeObject();
        $.ajax({
            type:"post",
            url:"/"+arg[1],
            data:JSON.stringify(data),
            contentType:"application/json;charset=utf-8",
            dataType:"json",
            success:function (data) {
                console.log(data);
                if(typeof(arg[2])!=="undefined"){
                    arg[2].val(data);
                }
            }
        });
    }
    function getProcessesByPage(page){
        $.ajax({
            type: "get",
            url: "/process/getProcesses",
            data: {pageNum:page},
            success: function (data) {
                var html='<thead><tr><th class="displayNone"></th><th>序号</th><th>乡镇名称</th><th>开始时间</th><th>结束时间</th></tr></thead><tbody>';
                for (var i = 0; i < data.content.length; i++) {
                    var obj = data.content[i];
                    html += `
					<tr><td class="displayNone">${obj.id}</td><td>${i + 1}</td><td>${obj.xsAreaName}</td><td>${obj.startTime}</td><td>${obj.endTime}</td></tr>`;
                }
                html += "</tbody>";
                m.table.html(html);
            }
        })
    }
    function getInfosByPage(page){
        $.ajax({
            type: "get",
            url: "/process/getInfos",
            data: {pageNum: page},
            success: function (data) {
                var html='<thead><tr><th class="displayNone"></th><th>序号</th><th>乡镇名称</th><th>开始时间</th><th>结束时间</th></tr></thead><tbody>';
                for (var i = 0; i < data.content.length; i++) {
                    var obj = data.content[i];
                    html += `
					<tr><td class="displayNone">${obj.id}</td><td>${i + 1}</td><td>${obj.xsAreaName}</td><td>${obj.startTime}</td><td>${obj.endTime}</td></tr>`;
                }
                html += "</Tbody>";
                m.table.html(html);
            }
        });
    }
    function getTypesByPage(page){
        $.ajax({
            type:"get",
            url:"/type/getTypes",
            data:{pageNum:page},
            success:function(data){
                var html='<thead><tr><th class="displayNone"></th><th>序号</th><th>灾害名称</th><th>特征描述</th><th>致灾原因</th></tr></thead><tbody>';
                for(var i=0;i<data.content.length;i++){
                    var obj=data.content[i];
                    html+=`
					<tr><td class="displayNone">${obj.id}</td><td>${i+1}</td><td>${obj.name}</td><td>${obj.signalment}</td><td>${obj.disasterReason}</td></tr>`;
                }
                html+="</tbody>";
                m.table.html(html);
            }
        });
    }
    function setForecast(){
        console.log(56);
        m.contentRight.children().hide();
        m.forecast.show();
        $.ajax({
            type:"get",
            url:"/forecast/queryAll",
            success:function(data){
                var html="";
                for(var i=0;i<data.length;i++){
                    var obj=data[i];
                    html+=`
                        <tr>
										<td class="displayNone">
											<input type="hidden" name="id" value="${obj.id}">
										</td>
										<td>
											<input type="text" name="name" class="form-control" readonly="readonly" value="${obj.name}">
										</td>
										<td>
											<input type="text" name="threshold" class="form-control" value="${obj.threshold}">
										</td>
										<td>
											<input type="button" value="更新" class="btn btn-info">
										</td>
									</tr>
                    `;
                }
                m.forecast.find("tbody").html(html);
            }
        })
    }





    $(".x-content .content-left ul li").each(function(index, el) {
		$(el).on("click", function() {
			$(".x-content .content-left ul li").removeClass("selected");
			$(this).addClass("selected");
			if(index == 0) {
                type.displayType(1);
			}
			if(index == 1) {
                process.display(1);
			}
			if(index == 2) {info.display(1)}
            if(index == 3) {type.add()}
			if(index == 4) {process.add()}
			if(index == 5){
                setForecast();
            }
		});
	});
	$(".typeFormBtn").click(function(e) {
	    e.preventDefault();
        //验证
        if(isEmpty(m.typeForm)===false){return false}
        m.maintain.children().hide();
        m.subtypeForm.show();
        m.disasterItem.html("");
        m.typeForm.find("input,textarea").removeClass("borderRed").removeAttr("placeholder");
        //数据
        submitForm(m.typeForm,"type/addType",$("#subtypeForm .parentId"));
        //成功后操作
        m.typeForm.find("input,textarea").val('');
        $(".alert-modal").text("灾害类型添加成功！").show ().delay(500).fadeOut(500);
	});
    $(".subtypeFormBtn").click(function(e) {
        e.preventDefault();
        //验证
        if(isEmpty(m.subtypeForm)===false){return false}
        m.maintain.children().hide();
        m.strengthForm.show();
        m.subtypeForm.find("input,textarea").removeClass("borderRed").removeAttr("placeholder");
        //数据
        submitForm(m.subtypeForm, "type/addSubtype", $("#strengthForm .parentId"));
        //成功后操作
        m.subtypeForm.find("input,textarea").not(".parentId").val('');
        $(".alert-modal").text("灾害子类型添加成功！").show().delay(500).fadeOut(500);

	    });
	$(".strengthFormBtn").click(function(e){
        e.preventDefault();
        //验证
        if(isEmpty(m.strengthForm)===false){return false}
        if(isInt(m.strengthForm)===false){return false}
        //数据
        submitForm(m.strengthForm,"type/addStrength");
        //成功后操作
        m.strengthForm.find("input,textarea").removeClass("borderRed").removeAttr("placeholder");
        m.strengthForm.find("input,textarea").not(".parentId").val('');
        $(".alert-modal").text("灾害强度添加成功！").show ().delay(500).fadeOut(500);


	});
	$(".processFormBtn").click(function(e) {
	    e.preventDefault();
	    //提交验证
	    if(isEmpty(m.processForm)===false){return false}
        if(isInt(m.processForm)===false){return false}
        if(compare(m.processForm.find(".dateStart"),m.processForm.find(".dateEnd"))===false){return false}

        m.maintain.children().hide();
        m.processInfoForm.show();
        m.disasterItem.html("");
        m.processForm.find("input,textarea").removeClass("borderRed").removeAttr("placeholder");
        //数据
        submitForm(m.processForm,"process/addProcess",$("#processInfoForm .parentId"));
        //成功后操作
        m.processForm.find("input,textarea").not(".parentId").not(".selectBoard input").val('');
        $(".alert-modal").text("灾害过程添加成功！").show ().delay(500).fadeOut(500);
        //灾情信息--三级联动列表
        $.ajax({
            type:"get",
            url:"/widget/linkageTypeName",
            success:function(data){
                var html="";
                for(var i=0;i<data.length;i++){
                    var obj=data[i];
                    html+=`<option value="${obj.id}">${obj.content}</option>`;
                }
                $("#disasterTypeId").html(html);
                //灾害子类型显示
                var id=$("#disasterTypeId").val();
                $.ajax({
                    type:"get",
                    url:"/widget/linkageSubtypeName",
                    data:{id:id},
                    success:function(data){
                        var html="";
                        for(var i=0;i<data.length;i++){
                            var obj=data[i];
                            html+=`<option value="${obj.id}">${obj.content}</option>`;
                        }
                        $("#disasterSubtypeId").html(html);
                        linkage("#disasterSubtypeId","linkageStrengthLevel","#disasterStrengthId");
                    }
                })
            }
        });
	});
	$(".processInfoFormBtn").click(function(e){
         e.preventDefault();
        //提交验证
        if(isEmpty(m.processInfoForm)===false){return false}
        if(isInt(m.processInfoForm)===false){return false}
        if(compare(m.processInfoForm.find(".dateStart1"),m.processInfoForm.find(".dateEnd1"))===false){return false}
        m.processInfoForm.find("input,textarea").removeClass("borderRed").removeAttr("placeholder");
        submitForm(m.processInfoForm,"process/addInfo");
        //成功后操作
        m.processInfoForm.find("input,textarea").not(".parentId,.selectBoard input").val("");
        $(".alert-modal").text("灾情信息添加成功！").show ().delay(500).fadeOut(500);
	});


	m.subtypeForm.find(".back").on("click", function() {
		m.maintain.children().hide();
		m.typeForm.show();
        m.disasterItem.html("新建灾害类型");
		return false;
	});
	m.strengthForm.find(".back").on("click", function() {
		m.maintain.children().hide();
		m.subtypeForm.show();
		return false;
	});
	m.processInfoForm.find(".back").on("click", function() {
		m.maintain.children().hide();
		m.processForm.show();
        m.disasterItem.html("新建灾情过程");
		return false;
	});


    //页码操作
    $(".next").click(function(){
        var pageNum=parseInt(m.currentPage.html());
        var totlePage=parseInt($(".totalPage").html());
        pageNum=pageNum+1>totlePage?totlePage:(pageNum+1);
        m.currentPage.html(pageNum);
        $(".x-search").find("input").val("");
        if(m.table.hasClass("disasterProcess")){
            getProcessesByPage(pageNum);
        }
        if(m.table.hasClass("disasterInfo")){
            getInfosByPage(pageNum);
        }
        if(m.table.hasClass("disasterType")){
            getTypesByPage(pageNum);
        }
    });
    $(".pre").click(function(){
        var pageNum=parseInt(m.currentPage.html());
        pageNum=(pageNum-1===0)?1:(pageNum-1);
        m.currentPage.html(pageNum);
        $(".x-search").find("input").val("");
        if(m.table.hasClass("disasterProcess")){
            getProcessesByPage(pageNum);
        }
        if(m.table.hasClass("disasterInfo")){
            getInfosByPage(pageNum);
        }
        if(m.table.hasClass("disasterType")){
            getTypesByPage(pageNum);
        }
    });
    $(".go").click(function() {
        m.xSearch.find("input").val("");
        var aimPage = parseInt($(".aimPage").val());
        var page=aimPage>m.totalPage.html()?m.totalPage.html():aimPage;
        m.currentPage.html(page);
        if (m.table.hasClass("disasterProcess")) {
            getProcessesByPage(page)
        }
        if (m.table.hasClass("disasterInfo")) {
            getInfosByPage(page)
        }
        if(m.table.hasClass("disasterType")){
            getTypesByPage(page);
        }
    });


    //三级联动
    function linkage(dom1,str,dom2){
        $(dom2).html("");
        var id=$(dom1).val();
        $.ajax({
            type:"get",
            url:"/widget/"+str,
            data:{id:id},
            success:function(data){
                var html="";
                for(var i=0;i<data.length;i++){
                    var obj=data[i];
                    html+=`<option value="${obj.id}">${obj.content}</option>`;
                }
                $(dom2).html(html);
            }
        })
    }
    //灾害子类型
    $("#disasterTypeId").change(function(){
        var id=$("#disasterTypeId").val();
        $.ajax({
            type:"get",
            url:"/widget/linkageSubtypeName",
            data:{id:id},
            success:function(data){
                var html="";
                for(var i=0;i<data.length;i++){
                    var obj=data[i];
                    html+=`<option value="${obj.id}">${obj.content}</option>`;
                }
                $("#disasterSubtypeId").html(html);
                linkage("#disasterSubtypeId","linkageStrengthLevel","#disasterStrengthId");
            }
        })

    });
    //灾害强度
    $("#disasterSubtypeId").change(function(){
        linkage("#disasterSubtypeId","linkageStrengthLevel","#disasterStrengthId");
    });




    //条件查询--灾害过程
    $(".x-search button").click(function(){
        $(".x-search").find(".schedule-box,.selectBoard").addClass("displayNone");
        var obj={};
        if($(".x-search .shuju").val()!==""){obj.name=$(".x-search .shuju").val()}
        if($(".x-search .dateTop").val()!==""){obj.time=$(".x-search .dateTop").val()}
        obj.pageNum=parseInt(m.currentPage.html());
        if(m.table.hasClass("disasterProcess")){$.ajax({
            type:"get",
            data:obj,
            url:"/process/getProcesses",
            success:function(data){
                var html='<thead><tr><th class="displayNone"></th><th>序号</th><th>乡镇名称</th><th>开始时间</th><th>结束时间</th></tr></thead><tbody>';
                for(var i=0;i<data.content.length;i++){
                    var obj=data.content[i];
                    html+=`
					<tr><td class="displayNone">${obj.id}</td><td>${i+1}</td><td>${obj.xsAreaName}</td><td>${obj.startTime}</td><td>${obj.endTime}</td></tr>`;
                }
                html+="</tbody>";
                m.table.html(html);
            }
        })}
        if(m.table.hasClass("disasterInfo")){$.ajax({
            type:"get",
            data:obj,
            url:"/process/getInfos",
            success:function(data){
                var html='<thead><tr><th class="displayNone"></th><th>序号</th><th>乡镇名称</th><th>开始时间</th><th>结束时间</th></tr></thead><tbody>';
                for(var i=0;i<data.content.length;i++){
                    var obj=data.content[i];
                    html+=`
					<tr><td class="displayNone">${obj.id}</td><td>${i+1}</td><td>${obj.xsAreaName}</td><td>${obj.startTime}</td><td>${obj.endTime}</td></tr>`;
                }
                html+="</Tbody>";
                m.table.html(html);
            }
        })}
    });


    //阈值设置
    m.forecast.on("click","input[type=button]",function(){
        var threshold=$(this).parent().prev().children().first().val();
        var id=$(this).parent().parent().children().first().children().first().val();
        forecast.update(id,threshold);
    })





    //页面跳转、传参--灾害过程
    m.maintain.on("click",".disasterProcess tr",function(){
        var processId=$(this).children().first().html();
        var pageNum=m.currentPage.html();
        location.href="detail.html?processId="+processId+"&pageNum="+pageNum;
    });
    m.maintain.on("click",".disasterInfo tr",function(){
        var infoId=$(this).children().first().html();
        var pageNum=m.currentPage.html();
        location.href="detail.html?infoId="+infoId+"&pageNum="+pageNum;
    });
    m.maintain.on("click",".disasterType tr",function(){
        var typeId=$(this).children().first().html();
        var pageNum=m.currentPage.html();
        location.href="detail.html?typeId="+typeId+"&pageNum="+pageNum;
    });


});









































